// Mock data for role management

// 模拟角色数据
const mockRoles = [
  {
    roleId: 'R001',
    roleName: '系统管理员',
    roleStatus: 'Y',
    mountStatus: 'Y',
    createTime: '2024-01-15 10:30:00',
    updateTime: '2024-10-10 15:20:00',
  },
  {
    roleId: 'R002',
    roleName: '普通用户',
    roleStatus: 'Y',
    mountStatus: 'Y',
    createTime: '2024-02-20 14:20:00',
    updateTime: '2024-09-28 11:15:00',
  },
  {
    roleId: 'R003',
    roleName: '访客',
    roleStatus: 'Y',
    mountStatus: 'N',
    createTime: '2024-03-10 09:15:00',
    updateTime: '2024-09-15 16:30:00',
  },
  {
    roleId: 'R004',
    roleName: '财务管理员',
    roleStatus: 'Y',
    mountStatus: 'Y',
    createTime: '2024-04-05 16:45:00',
    updateTime: '2024-10-01 09:10:00',
  },
  {
    roleId: 'R005',
    roleName: '人事专员',
    roleStatus: 'N',
    mountStatus: 'N',
    createTime: '2024-05-12 11:20:00',
    updateTime: '2024-08-20 14:25:00',
  },
  {
    roleId: 'R006',
    roleName: '项目经理',
    roleStatus: 'Y',
    mountStatus: 'Y',
    createTime: '2024-06-18 13:30:00',
    updateTime: '2024-10-05 10:40:00',
  },
  {
    roleId: 'R007',
    roleName: '技术支持',
    roleStatus: 'Y',
    mountStatus: 'Y',
    createTime: '2024-07-22 15:10:00',
    updateTime: '2024-09-30 13:50:00',
  },
  {
    roleId: 'R008',
    roleName: '销售主管',
    roleStatus: 'Y',
    mountStatus: 'N',
    createTime: '2024-08-08 10:00:00',
    updateTime: '2024-10-08 11:20:00',
  },
  {
    roleId: 'R009',
    roleName: '客服专员',
    roleStatus: 'N',
    mountStatus: 'N',
    createTime: '2024-09-01 14:30:00',
    updateTime: '2024-09-25 16:15:00',
  },
  {
    roleId: 'R010',
    roleName: '运维工程师',
    roleStatus: 'Y',
    mountStatus: 'Y',
    createTime: '2024-09-15 09:45:00',
    updateTime: '2024-10-10 10:30:00',
  },
];

// 模拟用户数据
const mockUsers = [
  {
    userId: 'U001',
    userName: '张三',
    orgNo: 'ORG001',
    role: '系统管理员',
    position: '高级工程师',
    gender: '男',
    language: '中文',
    level: 'P7',
    createTime: '2024-01-10 09:00:00',
  },
  {
    userId: 'U002',
    userName: '李四',
    orgNo: 'ORG002',
    role: '普通用户',
    position: '开发工程师',
    gender: '女',
    language: '中文',
    level: 'P6',
    createTime: '2024-02-15 10:30:00',
  },
  {
    userId: 'U003',
    userName: '王五',
    orgNo: 'ORG001',
    role: '项目经理',
    position: '项目经理',
    gender: '男',
    language: '英文',
    level: 'P8',
    createTime: '2024-03-20 11:20:00',
  },
  {
    userId: 'U004',
    userName: '赵六',
    orgNo: 'ORG003',
    role: '技术支持',
    position: '技术支持工程师',
    gender: '男',
    language: '中文',
    level: 'P5',
    createTime: '2024-04-25 14:15:00',
  },
  {
    userId: 'U005',
    userName: '孙七',
    orgNo: 'ORG002',
    role: '财务管理员',
    position: '财务主管',
    gender: '女',
    language: '中文',
    level: 'P7',
    createTime: '2024-05-30 15:40:00',
  },
  {
    userId: 'U006',
    userName: '周八',
    orgNo: 'ORG004',
    role: '销售主管',
    position: '销售经理',
    gender: '男',
    language: '英文',
    level: 'P7',
    createTime: '2024-06-10 10:00:00',
  },
  {
    userId: 'U007',
    userName: '吴九',
    orgNo: 'ORG001',
    role: '运维工程师',
    position: '运维专家',
    gender: '男',
    language: '中文',
    level: 'P8',
    createTime: '2024-07-15 09:30:00',
  },
  {
    userId: 'U008',
    userName: '郑十',
    orgNo: 'ORG003',
    role: '人事专员',
    position: 'HR专员',
    gender: '女',
    language: '中文',
    level: 'P6',
    createTime: '2024-08-20 13:45:00',
  },
  {
    userId: 'U009',
    userName: '刘一',
    orgNo: 'ORG002',
    role: '客服专员',
    position: '客服代表',
    gender: '女',
    language: '中文',
    level: 'P5',
    createTime: '2024-09-01 11:20:00',
  },
  {
    userId: 'U010',
    userName: '陈二',
    orgNo: 'ORG004',
    role: '访客',
    position: '实习生',
    gender: '男',
    language: '中文',
    level: 'P4',
    createTime: '2024-09-25 16:10:00',
  },
];

export default {
  // 角色列表查询
  'POST /api/anyics-front/role/condition': (req: any, res: any) => {
    const { roleName, roleStatus, mountStatus, current = 1, pageSize = 10 } = req.body;
    
    let filteredRoles = [...mockRoles];
    
    // 根据角色名称筛选
    if (roleName) {
      filteredRoles = filteredRoles.filter(role => 
        role.roleName.includes(roleName)
      );
    }
    
    // 根据角色状态筛选
    if (roleStatus) {
      filteredRoles = filteredRoles.filter(role => 
        role.roleStatus === roleStatus
      );
    }
    
    // 根据挂载状态筛选
    if (mountStatus) {
      filteredRoles = filteredRoles.filter(role => 
        role.mountStatus === mountStatus
      );
    }
    
    // 分页处理
    const start = (current - 1) * pageSize;
    const end = start + pageSize;
    const paginatedData = filteredRoles.slice(start, end);
    
    res.json({
      header: {
        gid: '888888',
        errorCode: '000000',
        errorMsg: '操作成功',
        success: true,
      },
      data: paginatedData,
      total: filteredRoles.length,
      current,
      pageSize,
    });
  },

  // 创建角色
  'POST /api/anyics-front/role/create': (req: any, res: any) => {
    const newRole = {
      roleId: `R${String(mockRoles.length + 1).padStart(3, '0')}`,
      ...req.body,
      createTime: new Date().toISOString().replace('T', ' ').substring(0, 19),
      updateTime: new Date().toISOString().replace('T', ' ').substring(0, 19),
    };
    
    mockRoles.unshift(newRole);
    
    res.json({
      header: {
        gid: '888888',
        errorCode: '000000',
        errorMsg: '创建成功',
        success: true,
      },
      data: newRole,
    });
  },

  // 更新角色
  'POST /api/anyics-front/role/update': (req: any, res: any) => {
    const { roleId, ...updateData } = req.body;
    
    const roleIndex = mockRoles.findIndex(role => role.roleId === roleId);
    
    if (roleIndex !== -1) {
      mockRoles[roleIndex] = {
        ...mockRoles[roleIndex],
        ...updateData,
        updateTime: new Date().toISOString().replace('T', ' ').substring(0, 19),
      };
      
      res.json({
        header: {
          gid: '888888',
          errorCode: '000000',
          errorMsg: '更新成功',
          success: true,
        },
        data: mockRoles[roleIndex],
      });
    } else {
      res.status(404).json({
        header: {
          gid: '888888',
          errorCode: '404',
          errorMsg: '角色不存在',
          success: false,
        },
      });
    }
  },

  // 删除角色
  'POST /api/anyics-front/role/delete/:id': (req: any, res: any) => {
    const roleId = req.params.id;
    
    const roleIndex = mockRoles.findIndex(role => role.roleId === roleId);
    
    if (roleIndex !== -1) {
      mockRoles.splice(roleIndex, 1);
      
      res.json({
        header: {
          gid: '888888',
          errorCode: '000000',
          errorMsg: '删除成功',
          success: true,
        },
      });
    } else {
      res.status(404).json({
        header: {
          gid: '888888',
          errorCode: '404',
          errorMsg: '角色不存在',
          success: false,
        },
      });
    }
  },

  // 用户列表查询（用于角色用户详情）
  'GET /api/anyics-front/user/condition': (req: any, res: any) => {
    const { userId, userName, current = 1, pageSize = 10 } = req.query;
    
    let filteredUsers = [...mockUsers];
    
    // 根据用户ID筛选
    if (userId) {
      filteredUsers = filteredUsers.filter(user => 
        user.userId.includes(userId as string)
      );
    }
    
    // 根据用户名筛选
    if (userName) {
      filteredUsers = filteredUsers.filter(user => 
        user.userName.includes(userName as string)
      );
    }
    
    // 分页处理
    const start = (Number(current) - 1) * Number(pageSize);
    const end = start + Number(pageSize);
    const paginatedData = filteredUsers.slice(start, end);
    
    res.json({
      header: {
        gid: '888888',
        errorCode: '000000',
        errorMsg: '操作成功',
        success: true,
      },
      data: paginatedData,
      total: filteredUsers.length,
      current: Number(current),
      pageSize: Number(pageSize),
    });
  },
};

